package Servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import DAO.DBCon;
import Model.EBInfo;

/**
 * Servlet implementation class EBISearch
 */
@WebServlet("/EBISearch")
public class EBISearch extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EBISearch() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBCon dbCon = new DBCon();
		Connection connection = dbCon.getConnection();
		// TODO Auto-generated method stub
		String con = new String(request.getParameter("con").getBytes("iso-8859-1"), "utf-8");
		
		String sql;
		if(con.equals("已婚") || con.equals("未婚")){
			sql = "select * from ebit91 where Mstatus=\"" + con + "\"";
		}else if(con.equals("高中") ||con.equals("大专")||con.equals("大学及以上")){
			sql = "select * from ebit91 where education=\"" + con + "\"";
		}else if(con.equals("在职") ||con.equals("辞退") ||con.equals("转出")||con.equals("辞职")||con.equals("退休")){
			sql = "select * from ebit91 where Pstatus=\"" + con + "\"";
		}
		else{
			sql = "select * from ebit91 where Pname=\"" + con + "\"";
		}
		
		ArrayList<EBInfo>eList = new ArrayList<EBInfo>();
		EBInfo ebi = null;
		
		try {
			PreparedStatement stmt = connection.prepareStatement(sql);
			ResultSet rs1 = stmt.executeQuery();
			
			while(rs1.next()){
				ebi = new EBInfo(rs1.getString("Dname"),rs1.getString("Pname"),rs1.getString("Pstatus"),rs1.getString("workID"), 
						rs1.getString("name"), rs1.getString("sex"), rs1.getString("nation"),rs1.getString("education"),
						rs1.getString("Mstatus"),rs1.getString("address"), rs1.getString("identify"));
						eList.add(ebi);
			}
			HttpSession se = request.getSession();
			se.setAttribute("eList", eList);
			se.setAttribute("con", con);
			response.setHeader("refresh", "1;url=EBISearch.jsp");
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
